﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace Quan_ly_du_an
{
    public partial class frmnguoigiutre : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
        public frmnguoigiutre()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        public void frmnguoigiutre_Load(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT * FROM Nguoigiutre", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nguoigiutre");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables["Nguoigiutre"];
            conn.Close();
            conn.Open();
            cmd = new SqlCommand("SELECT Nhom.Manhom,Nhom.Tennhom,Malop,Tenlop,Siso FROM Nhom,Quanlylop where Nhom.Manhom = Quanlylop.Manhom", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nhomlop");
            cmb = new SqlCommandBuilder(da);
            dataGridView2.DataSource = ds.Tables["Nhomlop"];
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    textBox1.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM Nguoigiutre", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "Nguoigiutre");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["Manguoigiutre"] = textBox1.Text;
                    newRow["Tennguoigiutre"] = textBox2.Text;
                    newRow["Ngaysinh"] = maskedTextBox1.Text;
                    newRow["SoDT"] = textBox3.Text;
                    newRow["Sotiet"] = textBox4.Text;
                    newRow["Luongcoban"] = textBox5.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "Nguoigiutre");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Người giữ trẻ đã có");
                frmNhom objfrmNhom = new frmNhom();
                this.Dispose();
                objfrmNhom.ShowDialog();
            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                da.Update(ds, "Nguoigiutre");
                MessageBox.Show("Sửa thành công");
            }
            catch (Exception)
            {
                MessageBox.Show("Bạn không thể sửa vì có tham chiếu khóa ngoại");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int i = dataGridView1.CurrentRow.Index;
                ds.Tables[0].Rows[i].Delete();
                da.Update(ds, "Nguoigiutre");
                MessageBox.Show("Xóa thành công");
            }
            catch (Exception)
            {
                MessageBox.Show("Bạn không thể xóa vì có khóa chính");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            maskedTextBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
           // textBox4.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát hay không  ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }


        private void button8_Click(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT Nhom.Manhom,Nhom.Tennhom,Malop,Tenlop,Siso FROM Nhom,Quanlylop where Nhom.Manhom = Quanlylop.Manhom and Nhom.Manhom like '%" + textBox7.Text + "%'", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nhomlop");
            cmb = new SqlCommandBuilder(da);
            dataGridView2.DataSource = ds.Tables["Nhomlop"];
            conn.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT Nhom.Manhom,Nhom.Tennhom,Malop,Tenlop,Siso FROM Nhom,Quanlylop where Nhom.Manhom = Quanlylop.Manhom and Quanlylop.Malop like '%" + textBox8.Text + "%'", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nhomlop");
            cmb = new SqlCommandBuilder(da);
            dataGridView2.DataSource = ds.Tables["Nhomlop"];
            conn.Close();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            frmnguoigiutre_Load(sender, e);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT * FROM Nguoigiutre where Manguoigiutre like '%" + textBox6.Text + "%'", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nguoigiutre");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables["Nguoigiutre"];
            conn.Close();
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT Nhom.Manhom,Nhom.Tennhom,Malop,Tenlop,Siso FROM Nhom,Quanlylop where Nhom.Manhom = Quanlylop.Manhom and Nhom.Manhom like '%" + textBox7.Text + "%'", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nhomlop");
            cmb = new SqlCommandBuilder(da);
            dataGridView2.DataSource = ds.Tables["Nhomlop"];
            conn.Close();
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT Nhom.Manhom,Nhom.Tennhom,Malop,Tenlop,Siso FROM Nhom,Quanlylop where Nhom.Manhom = Quanlylop.Manhom and Quanlylop.Malop like '%" + textBox8.Text + "%'", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nhomlop");
            cmb = new SqlCommandBuilder(da);
            dataGridView2.DataSource = ds.Tables["Nhomlop"];
            conn.Close();
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT * FROM Nguoigiutre where Manguoigiutre like '%" + textBox6.Text + "%'", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Nguoigiutre");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables["Nguoigiutre"];
            conn.Close();
        }
    }
}
